// @ts-ignore
// @ts-nocheck
/*
BOOL WINAPI CreateProcessInternalW(
  IN HANDLE                hUserToken,
  IN LPCWSTR               lpApplicationName,
  IN LPWSTR                lpCommandLine,
  IN LPSECURITY_ATTRIBUTES lpProcessAttributes,
  IN LPSECURITY_ATTRIBUTES lpThreadAttributes,
  IN BOOL                  bInheritHandles,
  IN DWORD                 dwCreationFlags,
  IN LPVOID                lpEnvironment,
  IN LPCWSTR               lpCurrentDirectory,
  IN LPSTARTUPINFOW        lpStartupInfo,
  IN LPPROCESS_INFORMATION lpProcessInformation,
  OUT PHANDLE              hNewToken 
);

BOOL CreateProcess
(
    LPCTSTR lpApplicationName,
    LPTSTR lpCommandLine,
    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    BOOL bInheritHandles,
    DWORD dwCreationFlags,
    LPVOID lpEnvironment,
    LPCTSTR lpCurrentDirectory,
    LPSTARTUPINFO lpStartupInfo,
    LPPROCESS_INFORMATIONlpProcessInformation
);
*/
let log = console.log

function main() {
  log('main')
  Interceptor.attach(Module.getExportByName('kernel32.dll', 'CreateProcessW'), {
    onEnter: function(args) {
      this.app = args[0].readUtf16String();
      this.cmd = args[1].readUtf16String();
      console.log('================   CreateProcessW', this.app, this.cmd);
      if(-1 != this.cmd.indexOf('Riot Client.exe')) {
        let new_cmd = this.cmd + ' --remote-debugging-port=29527';
        global.new_cmd = Memory.allocUtf16String(new_cmd);
        // args[1].replace(p);
        args[1] = global.new_cmd;
        log('================   cmd', args[1]);
      }
    },
    onLeave: function(retval) {
      log('================   retval', retval, global.new_cmd);
    }
  });
}

main();